﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Engine.Sgml.Schema;

namespace Engine.Sgml.Test.Util {
    public class DModelGroupsGenerator {
        private DElementGenerator elementGenerator = new DElementGenerator();

        public DModelGroups Generate(DModelGroupsConfiguration configuration) {
            DModelGroups model = new DModelGroups(configuration.OrderType, configuration.Occurrance);

            for (int i = 0; i < configuration.Length; i++) {
                if (configuration.GenerateElement) {
                    model.Add(elementGenerator.Generate(configuration.ElementConfiguration));
                    }
                else {
                    DModelGroupsConfiguration config = configuration.Random();
                    if (config.Depth == 0)
                        model.Add(elementGenerator.Generate(configuration.ElementConfiguration));
                    else if (model.Count > 0 && ((IModelGroupsItem)model.Last()).IsModelGroups()) {
                        int distance = configuration.DistanceBetweenInnerGroups;
                        while (i + distance >= configuration.Length - 1)
                            distance--;
                        if (distance == 0)
                            model.Add(Generate(config));
                        else {
                            while (distance != 0) {
                                model.Add(elementGenerator.Generate(configuration.ElementConfiguration));
                                distance--;
                                }
                            model.Add(Generate(config));
                            }
                        }
                    else
                        model.Add(Generate(config));
                    }
                }

            return model;
            }

        public DModelGroups GenerateInclusion(DModelGroupsConfiguration configuration) {
            DModelGroups model = new DModelGroups(DGroupOrderType.OnlyOne, configuration.Occurrance);
            int length = configuration.InclusionElementCount;
            for (int i = 0; i < length; i++) {
                model.Add(elementGenerator.GenerateInclusion(configuration.ElementConfiguration));
                }
            return model;
            }
        }
    }
